package http;

import frame.service.util.FrameUtil;
import util.SessionManager;

import java.util.HashMap;
import java.util.Map;

public class RedisSession {
    /**
     * 会话UUID
     * */
    private String sessionUUID;
    /**
     * 会话密钥
     * */
    private String sessionSecret;
    /**
     * 会话生成时的握手信息
     * */
    private String helloMsg;
    /**
     * 外部会话标识
     * 生成方式： sha256(sessionUUID+sessionSecret);
     * */
    private String sessionKey;
    private Map<String, String> sessionData = new HashMap<>(4);

    public RedisSession(){}

    public RedisSession(String sessionKey, String sessionUUID, String sessionSecret, String helloMsg){
        this.sessionKey = sessionKey;
        this.sessionSecret = sessionSecret;
        this.sessionUUID = sessionUUID;
        this.helloMsg = helloMsg;
        SessionManager.sessionUpdate(this);
    }

    public String getSessionUUID() {
        return sessionUUID;
    }

    public String getSessionSecret() {
        return sessionSecret;
    }

    public String getHelloMsg() {
        return helloMsg;
    }

    public String getSessionKey() {
        return sessionKey;
    }

    public void setSessionUUID(String sessionUUID) {
        this.sessionUUID = sessionUUID;
    }

    public void setSessionSecret(String sessionSecret) {
        this.sessionSecret = sessionSecret;
    }

    public void setHelloMsg(String helloMsg) {
        this.helloMsg = helloMsg;
    }

    public void setSessionKey(String sessionKey) {
        this.sessionKey = sessionKey;
    }

    public Map<String, String> getSessionData() {
        SessionManager.sessionValid(this);
        return sessionData;
    }

    public void setSessionData(Map<String, String> sessionData) {
        this.sessionData = sessionData;
        SessionManager.sessionUpdate(this);
    }

    public String getSessionData(String key){
        SessionManager.sessionValid(this);
        return sessionData.get(key);
    }
    public void setSessionData(String key, String data){
        sessionData.put(key, data);
        SessionManager.sessionUpdate(this);
    }

    public <T> T getSessionData(String key, Class t){
        SessionManager.sessionValid(this);
        String s = sessionData.get(key);
        if(s!=null){
            return FrameUtil.str2Obj(s, t);
        }
        return null;
    }


    public static class Constants{
        public static final String FUNC_PERMISSION = "__func_permission__";
        public static final String REQUEST_LIMIT_REPLAY = "__limit_replay__";
        /**
         * 限制业务场景下的请求服务链强关联
         * */
        public static final String REQUEST_CHAIN_LIMIT = "__limit_r_c__";
    }
}
